<template>
  <div class="wrapper">
    <div class="uc-main clearfix">
      <div class="uc-aside">
        <user-menu></user-menu>
      </div>
      <div class="uc-content">
        <div class="uc-panel">
          <div class="uc-bigtit">修改头像</div>
          <div class="uc-panel-bd">
            <div class="account-info clearfix" >
            <el-upload
              class="avatar-uploader col-userinfo"
              enctype="multipart/form-data"
              action="/java/api/upload"
              :headers="myHeaders"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
              <div class="control-submit">
              <input class="ui-btn-theme submit" @click="setHead" type="submit" value="修改" />
            </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import UserMenu from './UserMenu'
import Util from '../../../common/util'
import http from '../../../base/api/http'
export default {
  name: 'UserEmailWrapper',
  components: {
    UserMenu
  },
  data () {
    return {
      form: {
        imageUrl: ''
      },
      myHeaders: {
        Authorization: Util.getToken()
      }
    }
  },
  methods: {
    handleAvatarSuccess (res, file) {
      if (res.status === 0) {
        this.form.imageUrl = res.data
      } else {
        alert('上传失败')
      }
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    checkImage () {
      if (this.form.imageUrl === '') {
        this.form.imageUrl = 'http://mengyanxyz.asuscomm.com:20003/group1/M00/00/00/wKgyIFyFW_CAQKcSAACNVa1Yvyo529.jpg'
      }
    },
    setHead () {
      http.requestPost('/java/api/user/userHeadModify', this.form).then((res) => {
        if (res.status === 0) {
          alert('修改成功')
        } else {
          alert('修改失败')
        }
      })
    },
    getHead () {
      http.requestQuickGet('/java/api/user/getUserHead').then((res) => {
        if (res.status === 0) {
          this.form.imageUrl = res.data.imageUrl
        }
      })
    }
  },
  mounted () {
    this.getHead()
    this.checkImage()
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
